package wxpay

import (
	"log"
	"server/global"

	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/transferbatch"
)

// Transferbatch 商家转账
func Transferbatch(TotalAmount int64, Openid, OutBatchNo string) (resp *transferbatch.InitiateBatchTransferResponse, err error) {
	ctx, client, err := getWechatClient()
	if err != nil {
		return resp, err
	}

	svc := transferbatch.TransferBatchApiService{Client: client}
	resp, _, err = svc.InitiateBatchTransfer(ctx,
		transferbatch.InitiateBatchTransferRequest{
			Appid:       core.String(global.GL_CONFIG.WxJsApi.Appid),
			OutBatchNo:  core.String(OutBatchNo),
			BatchName:   core.String("观看直播奖励"),
			BatchRemark: core.String("直播奖励"),
			TotalAmount: core.Int64(TotalAmount),
			TotalNum:    core.Int64(1),
			TransferDetailList: []transferbatch.TransferDetailInput{transferbatch.TransferDetailInput{
				OutDetailNo:    core.String(OutBatchNo + "1"),
				TransferAmount: core.Int64(TotalAmount),
				TransferRemark: core.String("观看直播奖励"),
				Openid:         core.String(Openid),
				//UserName:       core.String(""),
				//UserIdCard:     core.String(""),
			}},
		},
	)
	if err != nil {
		// 处理错误
		log.Printf("call InitiateBatchTransfer err:%s", err)
	}
	return
}

//// Notice 回调
//func Notice(c *gin.Context) (error, *notify.Request, *payments.Transaction) {
//	wechatpayPublicKey, err := utils.LoadPublicKeyWithPath("apiclient/pub_key.pem")
//	if err != nil {
//		panic(fmt.Errorf("load wechatpay public key err:%s", err.Error()))
//	}
//
//	// 自定义验证器，增加时间宽容度
//	verifier := verifiers.NewSHA256WithRSAPubkeyVerifier(
//		global.GL_CONFIG.WxJsApi.WechatpayPublicKeyID,
//		*wechatpayPublicKey,
//	)
//
//	// 初始化 notify.Handler 并设置时间验证宽容度为5分钟（300秒）
//	handler := notify.NewNotifyHandler(
//		global.GL_CONFIG.WxJsApi.MchAPIv3Key,
//		verifier,
//	)
//
//	transaction := new(payments.Transaction)
//	notifyReq, err := handler.ParseNotifyRequest(context.Background(), c.Request, transaction)
//	// 如果验签未通过，或者解密失败
//	if err != nil {
//		// 可以在这里记录详细错误日志，方便排查
//		log.Printf("解析通知失败: %v, 请求信息: %+v", err, c.Request)
//		return err, nil, nil
//	}
//
//	return nil, notifyReq, transaction
//}
